/*******************************************************************************
 * Copyright (c) 2015, 2015 IBM Corp. and others
 *
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/
 * or the Apache License, Version 2.0 which accompanies this distribution and
 * is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * This Source Code may also be made available under the following
 * Secondary Licenses when the conditions for such availability set
 * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
 * General Public License, version 2 with the GNU Classpath
 * Exception [1] and GNU General Public License, version 2 with the
 * OpenJDK Assembly Exception [2].
 *
 * [1] https://www.gnu.org/software/classpath/license.html
 * [2] http://openjdk.java.net/legal/assembly-exception.html
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
 *******************************************************************************/
package com.ibm.j9ddr.corereaders.elf;

import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import com.ibm.j9ddr.corereaders.InvalidDumpFormatException;

/**
 * ELFDumpReader for ARM32 dumps.
 * 
 * @author hhellyer
 *
 */
public class ELFARM32DumpReader extends ELFDumpReader {

	ELFARM32DumpReader(ELFFileReader reader) throws IOException, InvalidDumpFormatException
	{
		super(reader);
	}
	
	@Override
	protected long readUID() throws IOException {
		return _reader.readShort() & 0xffffL;
	}

	@Override
	protected String getProcessorType() {
		return "arm";
	}

	@Override
	protected SortedMap<String, Number> readRegisters() throws IOException
	{
		// The first few register names are simple "r"+num
		SortedMap<String, Number> registers = new TreeMap<String, Number>(new RegisterComparator());
		for (int i = 0; i < 13; i++) {
			registers.put( "r"+i, Long.valueOf(_reader.readInt() & 0xFFFFFFFFL));
		}
		// The next registers have names
		registers.put( "sp", Long.valueOf(_reader.readInt() & 0xFFFFFFFFL));
		registers.put( "lr", Long.valueOf(_reader.readInt() & 0xFFFFFFFFL));
		registers.put( "pc", Long.valueOf(_reader.readInt() & 0xFFFFFFFFL));
		registers.put( "spsr", Long.valueOf(_reader.readInt() & 0xFFFFFFFFL));

		return registers;
	}

	@Override
	protected String getStackPointerRegisterName() {
		return "sp";
	}

	@Override
	protected long getBasePointerFrom(Map<String, Number> registers) {
		return getStackPointerFrom(registers);
	}

	@Override
	protected long getInstructionPointerFrom(Map<String, Number> registers) {
		return registers.get("pc").longValue();
	}

	@Override
	protected long getLinkRegisterFrom(Map<String, Number> registers) {
		return registers.get("lr").longValue();
	}

	@Override
	protected void readHighwordRegisters(DataEntry entry,
			Map<String, Number> registers) throws IOException,
			InvalidDumpFormatException {
		throw new InvalidDumpFormatException("Unexpected data entry in ARM ELF dump");
}

	@Override
	protected String[] getDwarfRegisterKeys() {
		// Needs to be overridden by each platform. Need to consult the platform ABI to map reg names to dwarf numbers.
		// (This list generated by looking at the order of the registers displayed by info registers in gdb)
		String[] registersVals = new String[16];
		for (int i = 0; i < 13; i++) {
			registersVals[i] = "r"+i;
		}
		registersVals[13] = "sp";
		registersVals[14] = "lr";
		registersVals[15] = "pc";
		registersVals[16] = "spsr";
		return registersVals;
	}

}
